home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1998 February / CD WARE MULTIMEDIA (02-1998) CD++.iso / Encript / SNOW / SOURCE.ZIP / snow / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-28  |  3.1 KB  |  199 lines

  1. /*
  2.  * Encryption routines for the SNOW steganography program.
  3.  * Uses the ICE encryption algorithm in 1-bit cipher-feedback (CFB) mode.
  4.  *
  5.  * Written by Matthew Kwan - December 1996
  6.  */
  7.  
  8. #include "snow.h"
  9. #include "ice.h"
  10.  
  11.  
  12. /*
  13.  * The key to use for encryption/decryption.
  14.  */
  15.  
  16. static ICE_KEY        *ice_key = NULL;
  17. static unsigned char    encrypt_iv_block[8];
  18.  
  19.  
  20. /*
  21.  * Build the ICE key from the supplied password.
  22.  * Only uses the lower 7 bits from each character.
  23.  */
  24.  
  25. void
  26. password_set (
  27.     const char    *passwd
  28. ) {
  29.     int        i, level;
  30.     unsigned char    buf[1024];
  31.  
  32.     level = (strlen (passwd) * 7 + 63) / 64;
  33.  
  34.     if (level == 0) {
  35.         if (!quiet_flag)
  36.         fprintf (stderr, "Warning: an empty password is being used\n");
  37.         level = 1;
  38.     } else if (level > 128) {
  39.         if (!quiet_flag)
  40.         fprintf (stderr, "Warning: password truncated to 1170 chars\n");
  41.         level = 128;
  42.     }
  43.  
  44.     if ((ice_key = ice_key_create (level)) == NULL) {
  45.         if (!quiet_flag)
  46.         fprintf (stderr, "Warning: failed to set password\n");
  47.         return;
  48.     }
  49.  
  50.     for (i=0; i<1024; i++)
  51.         buf[i] = 0;
  52.  
  53.     i = 0;
  54.     while (*passwd != '\0') {
  55.         unsigned char    c = *passwd & 0x7f;
  56.         int            idx = i / 8;
  57.         int            bit = i & 7;
  58.  
  59.         if (bit == 0) {
  60.         buf[idx] = (c << 1);
  61.         } else if (bit == 1) {
  62.         buf[idx] |= c;
  63.         } else {
  64.         buf[idx] |= (c >> (bit - 1));
  65.         buf[idx + 1] = (c << (9 - bit));
  66.         }
  67.  
  68.         i += 7;
  69.         passwd++;
  70.  
  71.         if (i > 8184)
  72.         break;
  73.     }
  74.  
  75.     ice_key_set (ice_key, buf);
  76.  
  77.         /* Set the initialization vector with the key
  78.          * with itself.
  79.          */
  80.     ice_key_encrypt (ice_key, buf, encrypt_iv_block);
  81. }
  82.  
  83.  
  84. /*
  85.  * Initialize the encryption routines.
  86.  */
  87.  
  88. void
  89. encrypt_init (void)
  90. {
  91.     encode_init ();
  92. }
  93.  
  94.  
  95. /*
  96.  * Encrypt a single bit.
  97.  */
  98.  
  99. BOOL
  100. encrypt_bit (
  101.     int        bit,
  102.     FILE        *inf,
  103.     FILE        *outf
  104. ) {
  105.     int        i;
  106.     unsigned char    buf[8];
  107.  
  108.     if (ice_key == NULL)
  109.         return (encode_bit (bit, inf, outf));
  110.  
  111.     ice_key_encrypt (ice_key, encrypt_iv_block, buf);
  112.     if ((buf[0] & 128) != 0)
  113.         bit = !bit;
  114.  
  115.             /* Rotate the IV block one bit left */
  116.     for (i=0; i<8; i++) {
  117.         encrypt_iv_block[i] <<= 1;
  118.         if (i < 7 && (encrypt_iv_block[i+1] & 128) != 0)
  119.         encrypt_iv_block[i] |= 1;
  120.     }
  121.     encrypt_iv_block[7] |= bit;
  122.  
  123.     return (encode_bit (bit, inf, outf));
  124. }
  125.  
  126.  
  127. /*
  128.  * Flush the contents of the encryption routines.
  129.  */
  130.  
  131. BOOL
  132. encrypt_flush (
  133.     FILE        *inf,
  134.     FILE        *outf
  135. ) {
  136.     ice_key_destroy (ice_key);
  137.  
  138.     return (encode_flush (inf, outf));
  139. }
  140.  
  141.  
  142. /*
  143.  * Initialize the decryption routines.
  144.  */
  145.  
  146. void
  147. decrypt_init (void)
  148. {
  149.     uncompress_init ();
  150. }
  151.  
  152.  
  153. /*
  154.  * Decrypt a single bit.
  155.  */
  156.  
  157. BOOL
  158. decrypt_bit (
  159.     int        bit,
  160.     FILE        *outf
  161. ) {
  162.     int        i;
  163.     int        nbit;
  164.     unsigned char    buf[8];
  165.  
  166.     if (ice_key == NULL)
  167.         return (uncompress_bit (bit, outf));
  168.  
  169.     ice_key_encrypt (ice_key, encrypt_iv_block, buf);
  170.     if ((buf[0] & 128) != 0)
  171.         nbit = !bit;
  172.     else
  173.         nbit = bit;
  174.  
  175.             /* Rotate the IV block one bit left */
  176.     for (i=0; i<8; i++) {
  177.         encrypt_iv_block[i] <<= 1;
  178.         if (i < 7 && (encrypt_iv_block[i+1] & 128) != 0)
  179.         encrypt_iv_block[i] |= 1;
  180.     }
  181.     encrypt_iv_block[7] |= bit;
  182.  
  183.     return (uncompress_bit (nbit, outf));
  184. }
  185.  
  186.  
  187. /*
  188.  * Flush the contents of the decryption routines.
  189.  */
  190.  
  191. BOOL
  192. decrypt_flush (
  193.     FILE        *outf
  194. ) {
  195.     ice_key_destroy (ice_key);
  196.  
  197.     return (uncompress_flush (outf));
  198. }
  199.